package nuc.sw.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Component;

import nuc.sw.dao.UserDAO;
import nuc.sw.entity.User;
@Component("userDAOImpl")
public class UserDAOImpl implements UserDAO{
	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void save(User user) throws Exception{
		hibernateTemplate.save(user);
		System.out.println("user "+user.getName()+" saved!");
	}

	public List<User> findAll() throws Exception {
		List<User> users = (List<User>)hibernateTemplate.find("from User");
		return users;
	}

	public void delete(User user) throws Exception {
		hibernateTemplate.delete(user);
		System.out.println(user.getName() + "has been deleted!");
	}

	public void update(User user) throws Exception {
		System.out.println("update info:" + user);
		hibernateTemplate.update(user);
		System.out.println("user" + user.getName() + "has been updated!");
	}

}
